package phone.qyg.com.qyg.fragment.course.detail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.base.tool.BaseFragment;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.JSONTool;
import com.app.utils.TransformController;
import com.app.view.BaseViewStateLayout;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.CourseDetail;
import phone.qyg.com.qyg.model.CourseVideo;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/11 16:53
 * 邮箱：lanbuhan@163.com
 * 功能：目录
 */

public class CatalogFragment extends BaseFragment {

    private static String courseID;//课程id

    @ViewInject(R.id.linear)
    LinearLayout linear;

    BaseViewStateLayout baseView;

    RecyclerView recycler;

    CourseVideo temp;

    public static final CatalogFragment newInstance(String courseID){
        CatalogFragment fragment = new CatalogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(AppHttpKey.COURSE_ID, courseID);
        CatalogFragment.courseID = courseID;
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater) {
        View view = View.inflate(QXApplication.getContext(), R.layout.course_video,null);
        x.view().inject(this,view);
        return view;
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        if (isRequestStr(courseID)) {
            courseID = getArguments().getString(AppHttpKey.COURSE_ID);
        }

        baseView = new BaseViewStateLayout(QXApplication.getContext()) {

            @Override
            public View obtionView() {
                View sucessView = View.inflate(QXApplication.getContext(), R.layout.base_recycler,null);
                recycler = (RecyclerView) sucessView.findViewById(R.id.recycler);
                LinearLayoutManager layoutVerManager = new LinearLayoutManager(QXApplication.getContext());
                layoutVerManager.setOrientation(LinearLayoutManager.VERTICAL);
                recycler.setLayoutManager(layoutVerManager);
                return sucessView;
            }

            @Override
            public Object obtionData() {
                return DELFAUTSTATE;
            }
        };

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linear.addView(baseView,layoutParams);
        x.view().inject(linear);

    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //可见时执行的操作
            getInitNetWork();
        }
    }


    private void getInitNetWork(){
        fMapKeys.put(AppHttpKey.TYPE,"1");
        fMapKeys.put(AppHttpKey.COURSE_ID,courseID);
        AppHttpRequest.httpRequestDefalut(ApplicationConfig.COURSE_DETAIL,
                fMapKeys,
                new AppHttpRequest.OnAppHttpListener() {
                    @Override
                    public void onAppHttpState(boolean isState, String result) {
                        if(isState && JSONTool.isStatus(result)){
                            CourseDetail courseDetail = JSONTool.jsonDefaluTranClazz(result,null, CourseDetail.class);
                            setData(courseDetail);
                        }else{
                            baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_ERROE);
                            baseView.stateView();
                        }
                    }
                });
    }

    private void setData(final CourseDetail model){
        if(null == model) return;

        List<CourseVideo> videos =  model.videoList;

        if(isRequestList(videos)){
            baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_SUCESS);
            baseView.stateView();

            final CommonAdapter adapter = new CommonAdapter<CourseVideo>(QXApplication.getContext(),R.layout.course_item_video,videos) {
                @Override
                protected void convert(ViewHolder holder, CourseVideo model, int position) {
                    holder.setBackgroundRes(R.id.item_layout,(position%2 == 0)?R.color.white:R.color.shallow_white);
                    holder.setText(R.id.item_title,setAttributeText(model.title));
                    holder.setTextDrawableLeft(R.id.item_select,(model.isStud == 1)?
                                                R.drawable.course_video_select:
                                                R.drawable.course_video_normal);
                    holder.setTextColorRes(R.id.item_title,(model.type == 1)?
                                                R.color.third_red:
                                                R.color.black);


                }
            };

            adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                    Object obj = adapter.getData().get(position);

                    if(obj instanceof CourseVideo){
                        final CourseVideo itemVideo = (CourseVideo) obj;


                        if(temp == itemVideo){
                            return;
                        }else{
                            itemVideo.type = 1;
                            if(null != temp){
                                temp.type = 0;
                            }
                            temp = itemVideo;
                            adapter.notifyDataSetChanged();
                        }

                        String result = JSON.toJSONString(itemVideo);
                        Intent intent = new Intent(ApplicationConfig.APP_QX_COURSE);
                        intent.putExtra(TransformController.ChangeKEY.INTENT_KEY, result);
                        LocalBroadcastManager.getInstance(QXApplication.getContext()).sendBroadcast(intent);

                        if(itemVideo.isStud == 1)
                            return;
                        AppHttpRequest.addCourseRecord(itemVideo.id, courseID, new AppHttpRequest.OnAppHttpListener() {
                            @Override
                            public void onAppHttpState(boolean isState, String result) {
                                if(isState){
                                    itemVideo.isStud = 1;
                                    adapter.notifyDataSetChanged();
                                }
                            }
                        });
                    }
                }

                @Override
                public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                    return false;
                }
            });

            recycler.setAdapter(adapter);

        }else{
            baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_EMPTY);
            baseView.stateView();
        }
    }


    @Override
    public void onNetChange(int netMobile) {}
}
